package com.woniuxy.house.provider;

import com.woniuxy.house.entity.Comment;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.SQL;

@Slf4j
public class CommentSqlProvider {
    public String update(Comment comment){
        String sql=new SQL(){
            {
                UPDATE("house_comment");
                if(comment.getTime()!=null){
                    SET("time=#{time}");
                }
                if(comment.getContent()!=null){
                    SET("content=#{content}");
                }
                if(comment.getUser()!=null){
                    SET("user_id=#{user.id}");
                }
                if(comment.getPost()!=null){
                    SET("post_id=#{post.id}");
                }
                WHERE("id=#{id}");
            }
        }.toString();
        log.debug("SQL:{}",sql);
        return sql;
    }

    public String selectByCondition(Comment comment){
        String sql=new SQL(){
            {
                SELECT("*");
                FROM("house_comment");
                if(comment.getTime()!=null && !comment.getTime().equals("")){
                    WHERE("time=#{time}");
                }
                if(comment.getContent()!=null && !comment.getContent().equals("")){
                    WHERE("content=#{context}");
                }
                if(comment.getUser()!=null && !comment.getUser().getAccount().equals("") && comment.getUser()!=null){
                    WHERE("user_id=#{user.id}");
                }
                if(comment.getPost()!=null && comment.getPost().getTitle()!=null && !comment.getPost().getTitle().equals("")){
                    WHERE("post_id=#{post.id}");
                }
            }

        }.toString();
        return  sql;
    }
//    SELECT * FROM house_comment WHERE user_id IN
//            (SELECT id FROM house_user WHERE ACCOUNT='用户1')
//    AND
//    post_id IN
//            (SELECT id FROM house_post WHERE title='cc')
    public String select(String account,String title){
        String sql=new SQL(){
            {
                SELECT("*");
                FROM("house_comment");
                if(!"".equals(account)&& account!=null){
                    WHERE("user_id in (select id from house_user where account=#{account})");
                }
                if(!"".equals(title)&& title!=null){
                    WHERE("post_id in (select id from house_post where title=#{title})");
                }
            }
        }.toString();
        return sql;
    }

}
